from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.shortcuts import render, redirect


# Create your views here.

#index
from app.models import UserProfile, Emp, Diary
from urllib import parse

def index(request):
    return render(request, 'index.html')
def admin(request):
    return render(request, 'admin.html')
def adminuser(request):
    #list=[{"acount":"344343","password":"whac4243","phone":"1565383645"}]
    userList=UserProfile.objects.all()
    context={"list":userList}
    return render(request, 'adminuser.html',context)

def adminemp(request):
    empList=Emp.objects.all()
    context={"list":empList}
    return render(request, 'adminEmployer.html',context)

def adminlog(request):
    logList=Diary.objects.all()
    context={"list":logList}
    return render(request, 'adminlog.html',context)


#退出管理页面
def adminout(request):
    return redirect('/app/')

#删除用户
def deleteuser(request):
    username=request.GET.get('username')
    User.objects.filter(username=username).delete()

    return redirect('/app/admin/employer/')

#新增员工
def addemp(request):
    empno=request.POST.get('empno')
    ename=request.POST.get('ename')
    job=request.POST.get('job')
    job=parse.unquote(job)
    print("job|"+job)
    salary=request.POST.get('salary')
    Emp.objects.create(empno=empno,ename=ename,job=job,salary=salary,judge=False)
    return redirect('/app/admin/employer/')

def empAdd(request):
    return render(request,'addEmp.html')
#删除员工
def deleteemp(request):
    empno=request.GET.get('empno')
    Emp.objects.filter(empno=empno).delete()

    return redirect('/app/admin/employer/')

#删除员工
def deletelog(request):
    tradeno=request.GET.get('tradeno')
    Diary.objects.filter(tradeno=tradeno).delete()

    return redirect('/app/admin/log/')

# 管理员登陆
def managerLogin(request):
    if request.POST:
        # 接收参数
        username = request.POST.get('manager', None)
        password = request.POST.get('password', None)
        if username == 'wuqilong' and password == 'wuqilong123':
            # 返回管理员操作界面
            #login(request, username)
            return redirect('/app/admin/')
            #return render(request,'admin.html')
        else:
            return render(request, 'managerLogin.html', {'msg': '登录验证失败,不存在该用户'})
    else:
        return render(request, 'managerLogin.html')


# 顾客登陆界面
@login_required
def userLogin(request):
    if request.POST:
        # 接收参数
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        request.session['username'] = username
        print('username='+username)
        print('password=' + password)

        # 去数据库进行认证
        # 说白了就是根据账号, 密码 去auth_user数据表进行匹配
        user = authenticate(username=username,password=password)
        j=User.objects.filter(username=username,password=password)
        if j.exists() or user is not None:
            # 存在该用户, 则进行登录
            # 这里的login函数, 主要是hi为了把登录的对象存入到django_session表
            login(request, user)

            # 响应到客户端
            # 返回到服务选择界面界面
            return render(request, 'selectService.html')
        else:

            return render(request, 'userLogin.html', {'msg': '登录验证失败,密码错误'})
    else:
        return render(request, 'userLogin.html')


# 用户注册
def createUser(request):
    if request.POST:
        # 获取参数
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        phone = request.POST.get('phone', None)
        request.session['phone'] = phone
        # 判断用户是否存在
        user = User.objects.filter(username=username)

        if len(user) > 0:
            return render(request, 'createUser.html', {'error_msg':'用户名已经被使用'})

        # 这是用户名可以使用的时候
        # 向django_user数据表种添加数据
        user = User.objects.create_user(username=username,password=password)
        #向userProfile表中添加数据
        profile = UserProfile()
        # 这是建立外键的一对一关系
        profile.user_id = user.id
        # 这是为phone属性赋值
        profile.phone = phone
        profile.save()

        return render(request, 'createUser.html', {'success_msg':'创建用户成功'})

    else:
        return render(request, 'createUser.html')

def selectService(request):
    return render(request, 'selectService.html')

def detail(request):
    job = request.GET.get('job')
    job = parse.unquote(job)
    empList = Emp.objects.filter(job=job)
    context = dict()
    context['empList'] = empList
    print(job + 'oasdo')
    return render(request, 'detail.html', context)

def employ(request,empno):
    Emp.objects.filter(empno=empno).update(judge=True)
    username=request.session.get("username",None)
    info=username+"_"+str(empno)
    # 把数据保存到数据库
    Diary.objects.create(info=info)
    return redirect('/app/emp/selectService/')





def cancelemploy(request,empno):
    Emp.objects.filter(empno=empno).update(judge=False)
    return redirect('/app/emp/selectService/')

# 安全退出
def myLogout(request):
    # 安全退出的函数
    # 主要是清除在django_session表中的登录对象数据
    logout(request)
    return redirect('/app/')

def showUser(request):
    username1 = request.session.get('username', None)
    user=User.objects.filter(username=username1)

    userList = UserProfile.objects.all()
    context = dict()
    context['userList'] = userList

    return render(request, 'user.html', context)
def update(request):
    return render(request,"updateUser.html")
def updateUser(request):
    if request.POST:
        # 接收参数
        password = request.POST.get('password', None)
        username1=request.session.get('username',None)
        phone=request.POST.get('phone',None)
        user=User.objects.get(username=username1)
        UserProfile.objects.filter(user=user).update(phone=phone)

        # 更新到数据库
        User.objects.filter(username=username1).update(password=password)

        return showUser(request)

